package network

import (
	"fmt"
	"gitee.com/amoyx/cloud-provider/pkg/cloud/aliyun/models"
	openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	util "github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
	vpc20160428 "github.com/alibabacloud-go/vpc-20160428/v6/client"
)

type RouteGreeter interface {
	Route(region string) RouteInterface
}

type RouteInterface interface {
	CreateRouteTable(args models.RouteTableRequest) (routeTableId string, err error)
	CreateRouteEntry(args models.RouteEntryRequest) (routeEntryId string, err error)
	DeleteRouteTable(routeTableId string) (err error)
	DeleteRouteEntry(routeTableId string, routeEntryId string) (err error)
}

type route struct {
	config *openapi.Config
	region string
	client *vpc20160428.Client
}

func newRoute(network *Network, region string) RouteInterface {
	v := &route{
		config: network.config,
		region: region,
	}
	endpoint := fmt.Sprintf("vpc.%s.aliyuncs.com", region)
	v.config.Endpoint = tea.String(endpoint)
	_client, _ := vpc20160428.NewClient(v.config)
	v.client = _client
	return v
}

// CreateRouteTable 创建路由表
func (r *route) CreateRouteTable(args models.RouteTableRequest) (routeTableId string, err error) {
	createRouteTableRequest := &vpc20160428.CreateRouteTableRequest{
		RegionId:       tea.String(r.region),
		VpcId:          tea.String(args.VpcId),
		RouteTableName: tea.String(args.RouteTableName),
		Description:    tea.String(args.Description),
	}
	runtime := &util.RuntimeOptions{}
	response, err := r.client.CreateRouteTableWithOptions(createRouteTableRequest, runtime)
	return *response.Body.RouteTableId, err
}

// DeleteRouteTable 删除路由表
func (r *route) DeleteRouteTable(routeTableId string) (err error) {
	request := &vpc20160428.DeleteRouteTableRequest{
		RegionId:     tea.String(r.region),
		RouteTableId: tea.String(routeTableId),
	}
	runtime := &util.RuntimeOptions{}
	_, err = r.client.DeleteRouteTableWithOptions(request, runtime)
	return
}

// CreateRouteEntry 创建路由条目
func (r *route) CreateRouteEntry(args models.RouteEntryRequest) (routeEntryId string, err error) {
	createRouteEntryRequest := &vpc20160428.CreateRouteEntryRequest{
		RegionId:             tea.String(r.region),
		RouteTableId:         tea.String(args.RouteTableId),
		DestinationCidrBlock: tea.String(args.DestinationCidrBlock),
		NextHopType:          tea.String(args.NextHopType),
		Description:          tea.String(args.Description),
		RouteEntryName:       tea.String(args.RouteEntryName),
		NextHopId:            tea.String(args.NextHopId),
	}
	runtime := &util.RuntimeOptions{}
	response, err := r.client.CreateRouteEntryWithOptions(createRouteEntryRequest, runtime)
	if err != nil {
		return
	}
	routeEntryId = *response.Body.RouteEntryId
	return
}

// DeleteRouteEntry 批量删除路由条目
func (r *route) DeleteRouteEntry(routeTableId string, routeEntryId string) (err error) {
	request := &vpc20160428.DeleteRouteEntryRequest{
		RegionId:     tea.String(r.region),
		RouteTableId: tea.String(routeTableId),
		RouteEntryId: tea.String(routeEntryId),
	}
	runtime := &util.RuntimeOptions{}
	_, err = r.client.DeleteRouteEntryWithOptions(request, runtime)
	return
}
